<script setup lang="ts">
import { getRoleDetailAPI, getRoleListAPI, getRoleUserAPI, getTreeListAPI } from "@/apis/system";
import type { Role, RoleData, RoleUser } from "@/types/system";
import { onMounted, ref } from "vue";
import userIcon from "@/assets/user.svg";
import userIconActive from "@/assets/user-active.svg";
import type { TabsPaneContext, TreeInstance } from "element-plus";
import { deleRoleAPI } from "@/apis/card";
const roleList = ref<Role[]>([]);
const getRoleList = async () => {
  const res = await getRoleListAPI();
  console.log("res", res);
  roleList.value = res.data;
  // console.log("RoleList", roleList.value);
  const roleId = res.data[0].roleId
  getRoleDetail(roleId as number)
};
onMounted(() => {
  getRoleList();
});

// 判断是否选中该下标
const isActive = ref<number>(0);


const treeList=ref<RoleData[]>([])
const getTreeList=async()=>{
  const res=await getTreeListAPI()
  treeList.value=res.data
  console.log('treeList.value',treeList.value);
  
}
getTreeList()
const treeRefs=ref<TreeInstance[]>([])
const changeRole = async (index: number, roleId: number) => {
  // 2.1 设置高亮效果
  isActive.value = index
  // 2.2 获取角色详情
  const res = await getRoleDetailAPI(roleId)
  console.log('res', res);
  perms.value=res.data.perms
  userTotal.value = res.data.userTotal 
  treeRefs.value.forEach((tree,index)=>{
    tree.setCheckedKeys(res.data.perms[index])
  })
  getRoleUser(roleId)
}
const perms = ref<number[][]>([])
const getRoleDetail = async (roleId: number) => {
  const res = await getRoleDetailAPI(roleId)
  getRoleDetail(roleId)
  
}

const handleClick = (tab: TabsPaneContext, event: Event) => {
  console.log(tab, event)
}
const userTotal = ref(0)
const activeTabName = ref<'tree' | 'member'>('tree')

// 6. 获取角色成员
const roleUserList = ref<RoleUser[]>([])
const getRoleUser = async (roleId: number) => {
  const res = await getRoleUserAPI(roleId)
  roleUserList.value = res.data.rows
}

const deleRole=(id:string)=>{
  deleRoleAPI(id)
  // 刷新页面
  getRoleList()
}
</script>

<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div
        v-for="(item, index) in roleList"
        :key="item.roleId"
        class="role-item"
        v-bind:class="{ active: isActive === index }"
        @click="changeRole(index,item.roleId as number)">
        <div class="role-info active">
          <img
            :src="index === isActive ? userIconActive : userIcon"
            class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <el-dropdown>
    <span class="el-dropdown-link">
      <img src="@/assets/more.svg" class="icon" />
    </span>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item  @click="$router.push(`/roleAdd/${item.roleId}`)">
          <el-button type="primary" link >编辑角色</el-button>
        </el-dropdown-item>
        <el-dropdown-item @click="deleRole(item.roleId!.toString())">
          <el-button link>删除</el-button></el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="small" @click="$router.push('/roleAdd')">
        添加角色
      </el-button>
      >
    </div>
    <!-- 右侧代码 -->
    <div class="right-wrapper">
      <el-tabs v-model="activeTabName">
        <el-tab-pane label="功能权限" name="tree">
          <div class="tree-wrapper">
          <div v-for="(item,index) in treeList" :key="item.id" class="tree-item">
            <div class="tree-title"> {{ item.title }} </div>
            <el-tree
              ref="treeRef"
              node-key="id"
              :data="item.children"
              :props="{ label: 'title', disabled: () => true }"
              show-checkbox
              default-expand-all
              :default-checked-keys="perms[index]"
              />
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane :label="`成员(${userTotal})`" name="member">
        <div class="user-wrapper">
            <el-table :data="roleUserList">
              <el-table-column type="index" width="250" label="序号" />
              <el-table-column prop="name" label="员工姓名" />
              <el-table-column prop="userName" label="登录账号" />
            </el-table>
          </div>
      </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<style scoped lang="scss">
.demo-tabs > .el-tabs__content {
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;

  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
        width: 20px;
      }
    }

    .more {
      display: flex;
      align-items: center;
      .icon {
        width: 15px;
      }
    }

    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;

        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>
